package com.project.controller;

import com.project.config.QuartzManager;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/quartz")
public class QuartzController {

    @Autowired
    private QuartzManager quartzManager;

    /**
     * 传入定时任务名称、组名、定时任务执行步长等参数
     * @param job 任务名称
     * @param group 任务组名
     * @param time 任务执行步长
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/modify")
    public String modify(@RequestParam("job")String job,   //job的名称
                         @RequestParam("group")String group,
                         @RequestParam("time")String time //job的组
    ) throws SchedulerException {
        // 开启定时任务
        quartzManager.startJob(job,group,time);
        // 打印定时任务信息
        String s = quartzManager.getjobInfo(job, group);
        log.info("job:"+s);
        return "ok";
    }

    @GetMapping("/delete")
    public String delete(@RequestParam("job")String job,   //job的名称
                         @RequestParam("group")String group
    ) throws SchedulerException {
        quartzManager.deleteJob(job,group);
        String s = quartzManager.getjobInfo(job, group);
        System.out.println("job:"+s);

        return "ok";
    }
}
